package com.raigor.application.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.raigor.common.config.RuoYiConfig;
import com.raigor.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.raigor.common.annotation.Log;
import com.raigor.common.core.controller.BaseController;
import com.raigor.common.core.domain.AjaxResult;
import com.raigor.common.enums.BusinessType;
import com.raigor.application.domain.FilePlugins;
import com.raigor.application.service.IFilePluginsService;
import com.raigor.common.utils.poi.ExcelUtil;
import com.raigor.common.core.page.TableDataInfo;

/**
 * 插件管理Controller
 *
 * @author xiaoshen
 * @date 2021-12-31
 */
@RestController
@RequestMapping("/system/plugins")
public class FilePluginsController extends BaseController
{
    @Autowired
    private IFilePluginsService filePluginsService;

    /**
     * 查询插件管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:plugins:list')")
    @GetMapping("/list")
    public TableDataInfo list(FilePlugins filePlugins) {
        startPage();
        String filePath = RuoYiConfig.getUploadPath();
        if (filePlugins.getPluginsFileAddress() != null) {
            filePlugins.setPluginsFileAddress(filePath + "/" + filePlugins.getPluginsFileAddress());
        } else {
            String username = SecurityUtils.getUsername();
            filePlugins.setPluginsFileAddress(filePath + "/" + username);
        }

        List<FilePlugins> list = filePluginsService.selectFilePluginsList(filePlugins);
        return getDataTable(list);
    }

    /**
     * 导出插件管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:plugins:export')")
    @Log(title = "插件管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FilePlugins filePlugins)
    {
        List<FilePlugins> list = filePluginsService.selectFilePluginsList(filePlugins);
        ExcelUtil<FilePlugins> util = new ExcelUtil<FilePlugins>(FilePlugins.class);
        util.exportExcel(response, list, "插件管理数据");
    }

    /**
     * 获取插件管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:plugins:query')")
    @GetMapping(value = "/{pluginsFileId}")
    public AjaxResult getInfo(@PathVariable("pluginsFileId") Long pluginsFileId)
    {
        return AjaxResult.success(filePluginsService.selectFilePluginsByPluginsFileId(pluginsFileId));
    }

    /**
     * 新增插件管理
     */
    @PreAuthorize("@ss.hasPermi('system:plugins:add')")
    @Log(title = "插件管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FilePlugins filePlugins)
    {
        return toAjax(filePluginsService.insertFilePlugins(filePlugins));
    }

    /**
     * 修改插件管理
     */
    @PreAuthorize("@ss.hasPermi('system:plugins:edit')")
    @Log(title = "插件管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FilePlugins filePlugins)
    {
        return toAjax(filePluginsService.updateFilePlugins(filePlugins));
    }

    /**
     * 删除插件管理
     */
    @PreAuthorize("@ss.hasPermi('system:plugins:remove')")
    @Log(title = "插件管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{pluginsFileIds}")
    public AjaxResult remove(@PathVariable Long[] pluginsFileIds)
    {
        AjaxResult ar=toAjax(filePluginsService.deleteFilePluginsByPluginsFileIds(pluginsFileIds));
        return ar;
    }
}
